package com.wing.storage.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.storage.model.entity.ProductStorageWorkOut;
import com.wing.storage.model.response.PurchaseOutExportVO;
import com.wing.storage.service.ProductStorageWorkOutService;
import com.wing.utils.ExcelUtils;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 仓库产品出库作业-Controller
 *
 * @author xiayang
 */
@Validated
@Api(value = "仓库产品出库作业管理", tags = "#/storage/productStorageWorkOut")
@RestController("productStorageWorkOutController")
@RequestMapping("storage/admin/productStorageWorkOut")
@RequiredArgsConstructor
public class ProductStorageWorkOutController {

    @Autowired
    private ProductStorageWorkOutService productStorageWorkOutService;

    @ApiOperation(value = "查询出库作业列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductStorageWorkOut> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "outTypeId", required = false) Long outTypeId,
            String receiptRecordCode,
            String startCreateTime,
            String endCreateTime
            
    ) {
        Page<ProductStorageWorkOut> pg = new Page<>(page, limit);
        Map<String, Object> map = new HashMap();
        map.put("code", code);
        map.put("outTypeId", outTypeId);
        map.put("receiptRecordCode", receiptRecordCode);
        map.put("startCreateTime", startCreateTime);
        map.put("endCreateTime", endCreateTime);

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if(StringUtils.isNotBlank(DynamicDataSource.name.get())){
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        map.put("databaseUser", databaseUser);
        IPage<ProductStorageWorkOut> list = productStorageWorkOutService.pageList(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加出库作业", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ProductStorageWorkOut productStorageWorkOut) {
        try{
             return productStorageWorkOutService.add(productStorageWorkOut);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "修改出库作业", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(ProductStorageWorkOut productStorageWorkOut) {
        productStorageWorkOutService.update(productStorageWorkOut);
        return JsonResult.success("修改成功！");
    }

    @ApiOperation(value = "删除出库作业", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        productStorageWorkOutService.del(id);
        return JsonResult.success("删除成功");
    }



    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(name = "code", required = false) String code,
                       @RequestParam(name = "outTypeId", required = false) String outTypeId
    ){
        List<PurchaseOutExportVO> list = new ArrayList<>();
        Map params = new HashMap();
        if(StringUtils.isNotBlank(code)){
            params.put("code", code);
        }
        if(StringUtils.isNotBlank(outTypeId)){
            params.put("outTypeId", outTypeId);
        }

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);

        list = productStorageWorkOutService.exportProductStoragePageList(params);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        // 生成带时间戳的文件名
        String fileName = "出库作业列表数据_" + timestamp;
        ExcelUtils.export(response,fileName,list, PurchaseOutExportVO.class);
    }

}
